using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Services;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;

namespace AiQiuQuan.Sport.WebApi.Controllers
{
    /// <summary>
    /// 比赛管理
    /// </summary>
    public class GameController : ApiController
    {
        private readonly IGameService _gameService;

        /// <summary>
        /// 比赛管理
        /// </summary>
        public GameController(
            IGameService gameService
            )
        {
            _gameService = gameService;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id">业务编号</param>
        [HttpGet("get")]
        public async Task<UnaryResult<GameDto>> GetAsync(Guid id)
        {
            var result = await _gameService.GetAsync(id);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 比赛详情
        /// </summary>
        [HttpPost("query")]
        public async Task<UnaryResult<GameDto>> QueryAsync(GameQueryRequestDto request)
        {
            var result = await _gameService.QueryAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 比赛分页请求
        /// </summary>
        [HttpPost("pagelist")]
        [AllowAnonymous]
        public async Task<UnaryResult<PageResult<GameDto>>> PageListAsync(GameQueryPageRequestDto request)
        {
            var result = await _gameService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost("add")]
        public async Task<UnaryResult> AddAsync(GameAddDto model)
        {
            var result = await _gameService.AddAsync(model);
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost("update")]
        public async Task<UnaryResult> UpdateAsync(GameUpdateDto model)
        {
            var result = await _gameService.UpdateAsync(model);
            return result;
        }

        /// <summary>
        /// 比赛状态修改
        /// </summary>
        [HttpPost("update_state")]
        public async Task<UnaryResult> UpdateGameStateAsync(GameStateUpdateDto request)
        {
            var result = await _gameService.UpdateGameStateAsync(request);
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost("delete")]
        public async Task<UnaryResult> DeleteAsync(Guid id)
        {
            var result = await _gameService.DeleteAsync(id);
            return result;
        }

        /// <summary>
        /// 比赛报名
        /// </summary>
        [HttpPost("join")]
        public async Task<UnaryResult<GameJoinResultDto>> JoinAsync(GameJoinRequestDto request)
        {
            var result = await _gameService.JoinAsync(request);
            return result;
        }

        /// <summary>
        /// 非团体比赛队伍列表
        /// </summary>
        [HttpPost("list_team_single")]
        public async Task<UnaryResult<List<GameTeamDto>>> GetGameTeamListAsync(GameTeamSingleRequestDto request)
        {
            var result = await _gameService.GetSingleGameTeamListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 团体比赛队伍概要列表
        /// </summary>
        [HttpPost("list_team_simple")]
        public async Task<UnaryResult<List<GameTeamSimpleDto>>> GetSimpleGameTeamListAsync(GameTeamRequestDto request)
        {
            var result = await _gameService.GetSimpleGameTeamListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 团体比赛队伍列表
        /// </summary>
        /// <param name="businessId">队伍业务ID</param>
        [HttpGet("list_team")]
        public async Task<UnaryResult<List<GameTeamDto>>> GetGameTeamListAsync([Required] Guid businessId)
        {
            var result = await _gameService.GetGameTeamListAsync(businessId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 小组信息更改
        /// </summary>
        [HttpPost("update_group")]
        public async Task<UnaryResult> UpdateGroupAsync(GameGroupUpdateDto request)
        {
            var result = await _gameService.UpdateGroupAsync(request);
            return result;
        }

        /// <summary>
        /// 更新队伍头像
        /// </summary>
        [HttpPost("update_team_headurl")]
        public async Task<UnaryResult> UpdateTeamHeadUrlAsync(GameTeamHeadUrlRequestDto request)
        {
            var result = await _gameService.UpdateTeamHeadUrlAsync(request);
            return result;
        }

        /// <summary>
        /// 结束小组赛
        /// </summary>
        /// <param name="groupId">小组ID</param>
        [HttpGet("finish_group")]
        public async Task<UnaryResult> FinishGameGroupAsync([Required] Guid groupId)
        {
            var result = await _gameService.FinishGameGroupAsync(groupId);
            return result;
        }

        /// <summary>
        /// 获取比赛小组列表
        /// </summary>
        [HttpPost("list_group")]
        public async Task<UnaryResult<List<GameGroupDto>>> GetGameGroupListAsync(GameGroupRequestDto request)
        {
            var result = await _gameService.GetGameGroupListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取小组信息及对阵信息列表
        /// </summary>
        [HttpPost("list_group_user_versus")]
        public async Task<UnaryResult<List<GameGroupDto>>> GetSingleGameGroupAndVersusInfo(GameGroupRequestDto request)
        {
            var result = await _gameService.GetGameGroupAndVersusListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取小组成员信息
        /// </summary>
        /// <param name="groupUserId">组成员ID</param>
        [HttpGet("get_group_user")]
        public async Task<UnaryResult<GameGroupUserDto>> GetGameGroupUserInfoAsync([Required] Guid groupUserId)
        {
            var result = await _gameService.GetGameGroupUserInfoAsync(groupUserId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 保存小组抽签
        /// </summary>
        [HttpPost("group_draw")]
        public async Task<UnaryResult> SaveGroupDrawLotsAsync(GameGroupDrawLotsRequestDto request)
        {
            var result = await _gameService.SaveGroupDrawLotsAsync(request);
            return result;
        }

        /// <summary>
        /// 小组比分输入
        /// </summary>
        [HttpPost("finish_group_versus")]
        public async Task<UnaryResult> FinishGroupVersusAsync(GameGroupVersusScoreAddDto request)
        {
            var result = await _gameService.FinishGroupVersusAsync(request);
            return result;
        }

        /// <summary>
        /// 小组排名计算
        /// </summary>
        [HttpPost("calculate_group_rank")]
        public async Task<UnaryResult> CalculateGroupRankAsync(GameGroupRankRequestDto request)
        {
            var result = await _gameService.CalculateGroupRankAsync(request);
            return result;
        }

        /// <summary>
        /// 淘汰赛比分输入
        /// </summary>
        [HttpPost("finish_konckout_versus")]
        public async Task<UnaryResult> FinishKnockoutVersusAsync(GameKnockoutVersusScoreAddDto request)
        {
            var result = await _gameService.FinishKnockoutVersusAsync(request);
            return result;
        }

        /// <summary>
        /// 获取比赛轮次列表
        /// </summary>
        [HttpPost("list_rounds")]
        public async Task<UnaryResult<List<GameKnockoutRoundsDto>>> GetGameRoundsListAsync(GameRequestDto request)
        {
            var result = await _gameService.GetGameRoundsListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 淘汰赛对阵列表
        /// </summary>
        [HttpPost("list_knockout_versus")]
        public async Task<UnaryResult<List<GameKnockoutVersusDto>>> GetGameKnockoutVersusListAsync(GameKnockoutVersusRequestDto request)
        {
            var result = await _gameService.GetGameKnockoutVersusListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 淘汰赛对阵信息修改
        /// </summary>
        [HttpPost("update_knockout_versus")]
        public async Task<UnaryResult> UpdateGameKnockoutVersusInfoAsync([Required] List<GameKnockoutVersusUpdateDto> request)
        {
            var result = await _gameService.UpdateGameKnockoutVersusInfoAsync(request);
            return result;
        }

        /// <summary>
        /// 结束淘汰赛轮次比赛
        /// </summary>
        /// <param name="knockoutRoundsId">轮次ID</param>
        [HttpGet("finish_konckout_rounds")]
        public async Task<UnaryResult> FinishKnockoutRoundsAsync([Required] Guid knockoutRoundsId)
        {
            var result = await _gameService.FinishKnockoutRoundsAsync(knockoutRoundsId);
            return result;
        }
    }
}
